<script setup>
import { ref, watch, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { queryPageApi, queryAllApi } from '@/api/user'

//性别列表数据
const genders = ref([{ name: '男', value: 1 }, { name: '女', value: 2 }])

const searchUser = ref({
  name: '',
  gender: '',
  date: [],
  begin: '',
  end: ''
})

//侦听searchUser中的date属性
watch(
  () => searchUser.value.date,
  (newValue, oldValue) => {
     if(newValue.length == 2){
      searchUser.value.begin = newValue[0]
      searchUser.value.end = newValue[1]
     }else {
      searchUser.value.begin = ''
      searchUser.value.end = ''
     }
  }
)

//声明token
const token = ref('')

onMounted(async () => {
  handleSearch()

  //加载localStorage存储的登录信息
  const loginUser = localStorage.getItem('loginUser');
  if(loginUser){
    token.value = JSON.parse(loginUser).token
  }
})

//查询用户
const handleSearch = async () => {

    const result = await queryPageApi(
      searchUser.value.name, 
      searchUser.value.gender, 
      searchUser.value.begin, 
      searchUser.value.end, 
      currentPage.value, 
      pageSize.value
    );
    
    if(result.code){
      userList.value = result.data.rows
      total.value = result.data.total
      ElMessage.success('查询成功')
    } else {
      ElMessage.error(result.msg || '查询失败')
    }
  }


const handleReset = () => {
  // 清空表单
  searchUser.value = {
    name: '',
    gender: '',
    date: [],
    begin: '',
    end: ''
  }
  currentPage.value = 1
  handleSearch()
}

// 示例数据
const userList = ref([])

// 分页配置
const currentPage = ref(1)
const pageSize = ref(5)
const total = ref(0)

// 分页处理
const handleSizeChange = (val) => {
  pageSize.value = val
  handleSearch()
}
const handleCurrentChange = (val) => {
  currentPage.value = val
  handleSearch()
}
</script>

<template>
  <h1>用户信息查询</h1> <br>
  <el-form :inline="true" :model="searchUser">
    <el-form-item label="姓名">
      <el-input v-model="searchUser.name" placeholder="请输入用户姓名"></el-input>
    </el-form-item>

    <el-form-item label="性别">
      <el-select v-model="searchUser.gender" placeholder="请选择" style="width: 120px;">
        <el-option label="男" value="1"></el-option>
        <el-option label="女" value="2"></el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="创建日期">
      <el-date-picker
        v-model="searchUser.date"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        value-format="YYYY-MM-DD"
      ></el-date-picker>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="handleSearch">查询</el-button>
      <el-button @click="handleReset">清空</el-button>
    </el-form-item>
  </el-form>

  <br><br>

  <!-- 表格 -->
  <el-table :data="userList" border style="width: 100%">
    <!-- 移除 selection 列 -->
    <el-table-column type="index" label="序号" width="100" align="center"/>
    <el-table-column prop="username" label="用户名" width="120" align="center"></el-table-column>
    <el-table-column prop="name" label="姓名" width="120" align="center"></el-table-column>
    <el-table-column label="性别" width="80" align="center">
      <template #default="scope" >
        {{ scope.row.gender == 1 ? '男' : '女' }}
      </template>
    </el-table-column>
    <el-table-column prop="phone" label="手机号" width="150" align="center"></el-table-column>
    <el-table-column prop="createTime" label="创建时间" width="210" align="center"></el-table-column>
    <el-table-column prop="updateTime" label="最后更新时间" width="210" align="center"></el-table-column>
  </el-table>
  <br>

  <!-- 分页 -->
  <el-pagination
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
    :current-page="currentPage"
    :page-sizes="[5, 10, 20, 30]"
    :page-size="pageSize"
    layout="total, sizes, prev, pager, next, jumper"
    :total="total"
  >
  </el-pagination>
</template>

<style scoped>
/* 保持与admin/user/index.vue相同的样式 */
</style>